#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2005;

int n, k, sz[N];
vector<pii> ch[N];
ll dp[N][N];
void dfs(int u, int fu) {
  sz[u] = 1;
  rep(i, 2, k) dp[u][i] = -1e18;
  for (pii p : ch[u]) {
    int v = p.first;
    ll w = p.second;
    if (v == fu) continue;
    dfs(v, u);
    sz[u] += sz[v];
    per(i, min(k, sz[u]), 0) {
      ll mx = -1e18;
      rep(j, 0, min(i, sz[v])) {
        ll b1 = j, b2 = k - j;
        ll w1 = sz[v] - j, w2 = (n - k) - w1;
        if (b2 > n - sz[v] || w2 > n - sz[v]) continue;
        mx = max(mx, dp[u][i - j] + dp[v][j] + w * (b1 * b2 + w1 * w2));
      }
      dp[u][i] = mx;
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  k = min(k, n - k);
  rep(i, 2, n) {
    int u, v, w;
    cin >> u >> v >> w;
    ch[u].push_back(pii(v, w));
    ch[v].push_back(pii(u, w));
  }
  dfs(1, 1);
  cout << dp[1][k];
  return 0;
}